package com.briup.day23;

/**
 * @author ThinkBook
 */
public class Test01_Deamon {
    public static void main(String[] args) {
        //1.创建我们的子线程1
        Thread th1 = new Thread("守护线程1"){
            @Override
            public void run() {
                String name = Thread.currentThread().getName();
                for(int i = 1; i <= 30; i++) {
                    System.out.println("in " + name + " run, i: " + i);

                    //在run方法 出现异常，必须 try-catch 捕获处理
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        //2.创建我们的子线程2
        Thread th2 = new Thread(new Runnable() {
            @Override
            public void run() {
            String name =Thread.currentThread().getName();
                for (int i = 100; i >= 50; i--) {
                    System.out.println("in " + name + " run, i: " + i);
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        },"守护线程2");
        //3.启动两个子线程
        th1.start();
        //设置守护线程
        th2.setDaemon(true);
        th2.start();

        //main线程继续运行
        for (char i='a';i<='z';i++){
            System.out.println("in " + Thread.currentThread().getName() + " run, i: " + i);
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}


